package com.seu.githubcn.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.seu.githubcn.config.Result;
import com.seu.githubcn.entity.Repo;
import com.seu.githubcn.entity.Tag;
import com.seu.githubcn.mapper.RepoMapper;
import com.seu.githubcn.mapper.TagMapper;
import org.eclipse.jgit.api.Git;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.Collections;

@RestController
@RequestMapping()
public class TagController {

    @Autowired
    TagMapper tagMapper;

    @Autowired
    RepoMapper repoMapper;

    @Value("${githubcn.base-path:/null}")
    String basePath;

    @GetMapping("{userName}/{repoName}/releases")
    public Result<?> tagList(HttpServletRequest request,
                             @PathVariable("userName") String userName,
                             @PathVariable("repoName") String repoName){
        var repo =
                repoMapper.selectOne(Wrappers.<Repo>lambdaQuery()
                        .eq(Repo::getOwnerName,userName)
                        .eq(Repo::getRepoName,repoName));
        var tagList =
                tagMapper.selectList(Wrappers.<Tag>lambdaQuery()
                        .eq(Tag::getRepoId,repo.getRepoId()));
        Collections.reverse(tagList);
        return new Result<>(200,tagList,"查询成功！");
    }

    @PostMapping("releases/new")
    public Result<?> newTag(@RequestBody Tag tag){
        if(tag.getTagVersion().contains(" ")){
            return new Result<>(400,null,"命名错误！");
        }
        var userName = tag.getOwnerName();
        var repoName = tag.getRepoName();
        var base = basePath.formatted(userName,repoName) + ".git";
        var repo =
                repoMapper.selectOne(Wrappers.<Repo>lambdaQuery()
                        .eq(Repo::getOwnerName,userName)
                        .eq(Repo::getRepoName,repoName));
        var ifExist = tagMapper.selectOne(Wrappers.<Tag>lambdaQuery()
                .eq(Tag::getRepoId,repo.getRepoId())
                .eq(Tag::getTagVersion,tag.getTagVersion()));
        if(ifExist!=null){
            return new Result<>(400,null,"已有发行版！");
        }

        var off = true;
        try {
            Git git = Git.open(new File(base));
            var tags = git.tagList().call();
            for(var one :tags){
                if(one.getName().equals(tag.getTagVersion())){
                    off=false;
                }
            }
            if(off){
                git.tag().setName(tag.getTagVersion()).call();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        Tag newOne = new Tag();
        newOne.setTagTitle(tag.getTagTitle());
        newOne.setDescription(tag.getDescription());
        newOne.setTagVersion(tag.getTagVersion());
        newOne.setRepoId(repo.getRepoId());
        tagMapper.insert(newOne);
        return new Result<>(200,null,"创建成功！");

    }

}
